<?php

defined('WX_ROOT') OR define('WX_ROOT', __DIR__.'/');

require_once (WX_ROOT.'WxBase.php');

/** 
 * 微信支付API类
 * @deprecated 
 * 		<p>所有接口失败返回FALSE,要获取失败信息请调用getErrors()
 * 		<p>所有接口可通过调用setParams($key,$val)设置更多参数
 */
class WxPayApi extends WxBase {
	/**
	 * 统一下单接口
	 * 	调用前请调用setParams()方法设置以下参数:
	 * 		<p>body:商品描述
	 * 		<p>out_trade_no:商户订单号
	 * 		<p>total_fee:总金额
	 * 		<p>spbill_create_ip:终端IP
	 * 		<p>notify_url:通知地址
	 * 		<p>trade_type:交易类型(APP)
	 * @return array
	 */
	public function unifiedorder(){
		$this->apiUrl = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
		$response = $this->request();
		if ( ! $response ){
			return FALSE;
		}
		
		$rtn = array();
		$rtn['prepayID'] = $this->response['prepay_id'];
		$appData  = array(
				"appid" => $this->config('appid'),
				"partnerid" => $this->config('mch_id'),
				"prepayid" => $this->response['prepay_id'],
				"noncestr" => $this->response['nonce_str'],
				"timestamp" => time(),
				'package' => 'Sign=WXPay',
		);
		$sign = parent::sign($appData,$this->config('key'));
		$appData['sign'] = $sign;
// 		unset($appData['appid']);
		
		$rtn['appData']  = $appData;
		return $rtn;
	}
	
	/**
	 * 查询订单交易状态
	 * @param string $out_trade_no	商户订单号
	 * @param string $transaction_id	微信订单号(优先使用)
	 */
	public function orderQuery($out_trade_no='',$transaction_id=''){
		$this->params = array();
		if ( empty($out_trade_no) && empty($transaction_id) ) {
			$this->errors['msg'] = 'transaction_id或out_trade_no必须传入一个';
			return FALSE;
		}
		if ( $transaction_id ) {
			$this->setParams('transaction_id',$transaction_id);
		}else{
			$this->setParams('out_trade_no',$out_trade_no);
		}
		$this->apiUrl = 'https://api.mch.weixin.qq.com/pay/orderquery';
		$response = $this->request();
		return $response;
	}
	/**
	 * 处理微信回调通知支付结果
	 * @return array
	 */
	public function notifyPayResult(){
		$xml = file_get_contents('php://input');
		$this->originalRespose = $xml;
		$rtn = self::XML2Array($xml);
		if( ! $rtn ){ 
			$this->setError('无输入数据');
			return FALSE; 
		}
		$sign = $rtn['sign'];
		unset($rtn['sign']);
		if ( $sign != self::sign( $rtn,$this->config('key') ) ) {
			$this->setError('签名错误');
// 			return FALSE;
		}
		return $rtn;
	}
	
	
	
	
	
	
	
}

